package com.sharedlease.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sharedlease.entity.R;
import com.sharedlease.entity.Comment;
import com.sharedlease.service.CommentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 吴钟富
 * @since 2021-03-02
 */
@RestController
@Api(tags = "评论相关接口")
@RequestMapping("/comment")
public class CommentController {
    @Autowired
    public CommentService commentService;

    @GetMapping("/getById/{id}")
    @ApiOperation("根据一个id查询评论")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "要查询的评论id", defaultValue = "1"),

    }
    )
    public Comment getById(@PathVariable Long id){
        Comment comment = commentService.getById(id);
        return comment;
    }
    @GetMapping("/getByUid/{uid}")
    @ApiOperation("根据uid查询评论")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uid", value = "要查询的评论的uid", defaultValue = "1"),

    }
    )
    public List<Comment> getByUid(@PathVariable Long uid){
        QueryWrapper<Comment> wrapper = new QueryWrapper<>();
        wrapper.eq("uid",uid);
        List<Comment> commentS = commentService.list(wrapper);
        return commentS;
    }
    @GetMapping("/list")
    @ApiOperation("查询全部评论")
    public List<Comment> list(){
        List<Comment> list = commentService.list(null);
        return list;
    }

    @PostMapping("/save")
    @ApiOperation("新增一条评论")
    public R save(@RequestBody Comment comment) {

        return R.operate(commentService.save(comment));
    }

    @PutMapping("/update")
    @ApiOperation("根据id更改一条评论")
    R update(@RequestBody Comment comment){
        if(commentService.updateById(comment)){
            return R.ok();
        }
        return R.error();
    }

    @DeleteMapping("/removeById/{id}")
    @ApiOperation("根据id删除一条评论")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "要删除的评论id", defaultValue = "-1"),

    }
    )
    R removeById(@PathVariable Long id) {

        return R.operate (commentService.removeById(id));
    }


}

